<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/8
 * Time: 13:46
 */
namespace app\blog\controller;
use cmf\controller\AdminBaseController;
use app\blog\model\GoodsModel;
use app\portal\model\PortalCategoryModel;
use think\Db;

class GoodstableController extends AdminBaseController
{
    /*
     * 获取登录信息
     * */
    public function session( )
    {
        $session=cmf_get_current_admin_id();
        $where['id']=$session;
        $store=DB::name('user')->where($where)->field('store_id')->find();
        $store_id=$store['store_id'];
        return $store_id;
    }
    //商品列表页面
    //下拉表的条件选择上  HTML页面中下拉框的name于数据表里面的类型相对应即可
    public function index()
    {
        /**搜索条件**/
        $session = $this->session();
        $goodsname = $this->request->param('g_goodsname');
        $code = trim($this->request->param('g_code'));
        $categoryId = $this->request->param('stortype');
        $where = array();
        if ($session != 0)
        {
            $where['storeid']=$session;
        }
        if ($goodsname) {
            $where['g.g_goodsname'] = ['like', "%$goodsname%"];
        }
        if($categoryId)
        {
            $where['g.stortype'] = $categoryId;
        }
        $goods = Db::name('goods_table')->alias('g')->join('store_type s', 'g.stortype = s.id')->field('g.*,s.cat_name,s.id')
        ->where($where)
        ->order("g.g_id DESC")
        ->paginate(10);
        $goods->appends($where);
        // 获取分页显示
        $page = $goods->render();
        //获取店铺类型
        $cause=array();
        if($categoryId)
        {
            $cause['id']=$categoryId;
        }
        $storetype=DB::name('store_type')->where($cause)->select();
        $type=array();
        $data['id']=0;
        $data['cat_name']='全部';
        if(!$categoryId){
            $type['0']=$data;
        }
        foreach($storetype as $k=>$v)
        {
            $k=$k+1;
            $type[$k]=$v;

        }
        $this->assign('stortype',$type);
        $this->assign("page", $page);
        $this->assign("data", $goods);
        return $this->fetch();
    }
    //新增
    public function add()
    {
        //获取规格
        $brand=DB::name('format')->where(['status'=>1])->select();
        //获取类别
        $classify=Db::name('GoodsClassify')->where(array('status'=>1))->select();
        $this->assign("classify", $classify);
        $this->assign("brand", $brand);
        return $this->fetch('goods/goodsadd');
    }
    //编辑
    public function edit()
    {
        $id = $this->request->param("g_id");
        $data=Db::name('Goods_table')->where(array('g_id'=>$id))->find();
        //获取单位
        $unit = Db::name('Unit')->where(array('status'=>1))->select();
        //获取类别
        $classify=Db::name('GoodsClassify')->where(array('status'=>1))->select();        
        //系列
        $brand = Db::name('brand')->where(array('status'=>1))->select();
        $this->assign("brand", $brand);
        $this->assign("unit", $unit);
        $this->assign("classify", $classify);        
        $this->assign("data", $data);
        return $this->fetch();
    }
    /*
    * 一键更新
    * 查询该商品模板类型（店铺类型 模板状态）
     * 判断goods表里面是否已经存在该商品
    * 从店铺类型中去查找店铺表
    * 然后对该种类型商店全部添加商品
    * */
    public function updates()
    {
        //$store=new store();
        $id  = $this->request->param('id');
        $goods=Db::name('goods_table')->where(array('g_id'=>$id))->find();
        $casue['g_tableid']=$goods['g_id'];
        $casue['stortype']=$goods['stortype'];
        $date=DB::name('goods')->where($casue)->select()->toArray();
        if($date)
        {
            $this->error('该商品已经存在不在更新');
        }else{
            $where['cat_store']=$goods['stortype'];
            $store=DB::name('store')->where($where)->select()->toArray();
            if($store)
            {
                $good=array();
                foreach ($store as $k=>$v)
                {
                    $good[$k]['g_tableid']=$goods['g_id'];
                    $good[$k]['g_goodsname']=$goods['g_goodsname'];
                    $good[$k]['g_pic']=$goods['g_pic'];
                    $good[$k]['g_format']=$goods['g_format'];
                    $good[$k]['g_price']=$goods['g_price'];
                    $good[$k]['g_category']=$goods['g_category'];
                    $good[$k]['g_vipprice']=$goods['g_vipprice'];
                    $good[$k]['g_status']=$goods['g_status'];
                    $good[$k]['g_ctime']=$goods['g_ctime'];
                    $good[$k]['g_utime']=$goods['g_utime'];
                    $good[$k]['num']=$goods['num'];
                    $good[$k]['storeid']=$v['id'];
                    $good[$k]['brandid']=$goods['brandid'];
                    $good[$k]['stortype']=$goods['stortype'];
                }
                $res=DB::name('goods')->insertAll($good);
                if($res){
                    $this->success('添加成功','goods/index');
                }else{
                    $this->error('添加失败','goods/add');
                }
            }else{
                $this->error('没有该类型实际店铺');
            }
        }


    }
    /*
    * 2018/05/25 王磊 根据系列添加商品
    * */
    public function goodsadd(){
        //获取规格
        $brand=DB::name('format')->where(['status'=>1])->select();
        //获取类别
        $classify=Db::name('GoodsClassify')->where(array('status'=>1))->select();
        //获取传值id
        $id=$this->request->param();
        $this->assign('id',$id['id']);
        $this->assign("classify", $classify);
        $this->assign("brand", $brand);
        return $this->fetch('goodstable/goodsadd');
    }
    /*
     * 2018-05-30  原价不能为0 原价为0的话不进行商品存储
     *
     * */
    public function goodspost(){
        $format=$_POST['data'];
        $array=array();
        foreach($format as $k=>$v)
        {
            $formats=explode(',',$v);
            if($formats[1]==0)
            {
                continue;
            }
            $array[$k]['g_format']=$formats[0];
            $array[$k]['g_price']=$formats[1];
            $array[$k]['g_vipprice']=$formats[2];
            $array[$k]['g_goodsname']=$_POST['goodsname'];
            $array[$k]['g_category']=$_POST['g_category'];
            $array[$k]['g_pic']=$_POST['pic'];
            $array[$k]['g_status']=1;
            $array[$k]['g_ctime']=time();
            $array[$k]['brandid']=$_POST['brand'];
            $array[$k]['pinyin']=$_POST['pinyin'];
            $array[$k]['num']=0;
        }

        $re=DB::name('goods_table')->insertAll($array);
        if($re){
            $date['status']=1;
            $date['con']='添加成功！';
            $this->success($date);
        }else{
            $date['status']=0;
            $date['con']='添加失败！';
            $this->error($date);
        }

    }
    //删除商品
    public function delete()
    {
        $id = $this->request->param("g_id");
        $info = Db::name('goods_table')->where(['g_id' => $id])->find();
        if ($info){
            $status = Db::name('goods_table')->delete($id);
            if (!empty($status)) {
                $this->success("删除成功！", url('Goods/index'));
            } else {
                $this->error("删除失败！");
            }
        }else{
            $this->error("该商品不存在！");
        }
    }
    public function upStatus()
    {
        $intId     = $this->request->param("g_id");
        $intStatus = $this->request->param("g_status");
        $intStatus = $intStatus ? 1 : 0;
        if (empty($intId)) {
            $this->error(lang("请选择要修改数据"));
        }
        $GoodsModel = new GoodsModel();
        $data['g_status'] =$intStatus;
        $data['g_id'] =$intId;
        $state=DB::name('goods_table')->where(['g_id'=>$intId])->update($data);
        if($state)
        {
            $this->success(lang("修改成功！"));
        }else{
            $this->error(lang("修改失败！"));
        }
    }   
}